package com.example.flutter_demo;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "com.example.flutter_demo/getApp";
    private MethodChannel methodChannel;

    @Override
    public void configureFlutterEngine(FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        methodChannel = new MethodChannel(flutterEngine.getDartExecutor(), CHANNEL);
        methodChannel.setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                        if (call.method.equals("Toast")) {
                            String text = call.argument("flutter");
                            Toast.makeText(MainActivity.this, "调用原生toast："+text, Toast.LENGTH_SHORT).show();
                        } else if (call.method.equals("Data")) {
                            result.success("Android传递过来的数据");//回调给flutter的参数
                        } else if (call.method.equals("JumpActivity")) {
                            String text = call.argument("flutter");
                            Intent intent=new Intent(MainActivity.this,FlutterToAndroid.class);
                            intent.putExtra("params1",text);
                            startActivity(intent);
                        } else {
                            //没有对应方法
                            result.notImplemented();
                        }
                    }
                }
        );
    }


}
